package org.budo.support.dao.page;

import java.io.Serializable;
import java.util.List;

import org.budo.support.lang.util.NumberUtil;

/**
 * 作为分页查询时的返回结果
 * 
 * @author lmw
 * @see org.budo.support.dao.page.Page
 */
public class PageModel<T> implements Serializable {
    private static final long serialVersionUID = 3015209047580315558L;

    private Integer recordCount;

    private List<T> list;

    public PageModel() {
    }

    public PageModel(List<T> list, Number recordCount) {
        this.setList(list);

        Integer _recordCount = NumberUtil.toInteger(recordCount);
        this.setRecordCount(_recordCount);
    }

    public PageModel(List<T> list, Page page) {
        this.setList(list);

        if (null == page || null == page.getRecordCount()) {
            return;
        }

        Integer recordCount = page.getRecordCount();
        this.setRecordCount(recordCount);
    }

    public PageModel(List<T> list) {
        this.setList(list);

        Integer recordCount = recordCount(list);
        this.setRecordCount(recordCount);
    }

    public static Integer recordCount(List<?> list) {
        if (null == list) {
            return null;
        }

        if (list instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page page = (com.github.pagehelper.Page) list;
            return new Integer((int) page.getTotal());
        }

        return list.size();
    }

    public Integer getRecordCount() {
        return this.recordCount;
    }

    public void setRecordCount(Integer recordCount) {
        this.recordCount = recordCount;
    }

    public List<T> getList() {
        return this.list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Boolean isEmpty() {
        return null == this.getList() || this.getList().isEmpty();
    }

    /**
     * 空的数组或者数组里面的全是空的
     */
    public static Boolean isEmpty(PageModel... list) {
        if (null == list || list.length < 1) {
            return true; // 本身就是空的
        }

        for (PageModel each : list) {
            if (!each.isEmpty()) {
                return false; // 发现非空的
            }
        }

        // 全都是空的
        return true;
    }

    public String toString() {
        return super.toString() + ", recordCount=" + this.getRecordCount() + ", list=" + this.getList();
    }
}